package com.eeepay.eeepay_v2.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.eeepay.eeepay_v2.R;
import com.eeepay.eeepay_v2._recadapter.ApplyforInfoListRAdapter;
import com.eeepay.eeepay_v2._recadapter.CommRecyclerBaseAdater;
import com.eeepay.eeepay_v2._recadapter.CommonLinerRecyclerView;
import com.eeepay.eeepay_v2.activity.ApplyforDetailedInfoActivity;
import com.eeepay.eeepay_v2.api.GetTerminalApplyRecordAPI;
import com.eeepay.eeepay_v2.api.TerminalNoTypeDef;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.List;

import cn.eeepay.api.grpc.nano.ManagementCenterProto;

/**
 * 描述：机具申请列表页面
 * 作者：zhuangzeqin
 * 时间: 2017/7/19-17:35
 * 邮箱：zzq@eeepay.cn
 */
public class ApplyforDevicesFragment extends ABBaseFragment {
    private CommonLinerRecyclerView lv_record;//采用RecyclerView
    private RefreshLayout refreshLayout;//刷新加载
    private int currPage = 1;//当前页
    private int mTotal = 0;//总的记录数
    private ApplyforInfoListRAdapter adapter;//适配器
//    private List<ManagementCenterProto.TerminalApplyRecord> terminalApplyRecords ;
    //add by zhuangzeqin 2017年8月10日15:39:31 获取机具申请记录,待处理，已处理，全部API 请求
    private GetTerminalApplyRecordAPI mTerminalApplyRecordAPI;

    private String mRecordStatus;//机具申请记录,待处理，已处理，全部状态

    /**
     * 创建实例
     * 机具申请记录的状态(DCL:待处理，YCL:已处理，QB:全部)
     * @param recordStatus
     * @return
     */
    public static ApplyforDevicesFragment getInstance(@TerminalNoTypeDef.TerminalNoType final String recordStatus) {
        ApplyforDevicesFragment applyforDevicesFragment = new ApplyforDevicesFragment();
        Bundle bundle = new Bundle();
        bundle.putString("recordStatus", recordStatus);
        applyforDevicesFragment.setArguments(bundle);
        return applyforDevicesFragment;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mTerminalApplyRecordAPI = new GetTerminalApplyRecordAPI();
        mTerminalApplyRecordAPI.setResultCallBack(resultCallBack);
    }

    /**
     * 数据接口回调监听
     */
    final GetTerminalApplyRecordAPI.ResultCallBack resultCallBack = new GetTerminalApplyRecordAPI.ResultCallBack() {
        @Override
        public void onFail(String msg) {
            showToast(msg);
        }

        @Override
        public void onSucess(int currPage, int total, List<ManagementCenterProto.TerminalApplyRecord> terminalApplyRecords) {
            mTotal = total;//获取总的记录数
            if (currPage==1) {
                adapter.setList(terminalApplyRecords);
            }
            else {
                adapter.addItemFromList(terminalApplyRecords);
            }
        }
    };

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_applyfordevices, null);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Bundle arguments = getArguments();
        mRecordStatus = arguments.getString("recordStatus",TerminalNoTypeDef.KEY_ALL);
        Log.d("ApplyforDevicesFragment", "onViewCreated: "+mRecordStatus);
        adapter = new ApplyforInfoListRAdapter(mContext);
        adapter.setOnItemClickLitener(new CommRecyclerBaseAdater.OnItemClickLitener<ManagementCenterProto.TerminalApplyRecord>() {
            @Override
            public void onItemClick(View view, ManagementCenterProto.TerminalApplyRecord item, int position) {
                Intent intent = new Intent(mContext, ApplyforDetailedInfoActivity.class);
                String needOperation = item.needOperation;//是否需要处理(Y需要/N不需要)
                String record_id = item.id;//记录id,需要传给后台处理的
                String createTime = item.createTime;//申请时间
                String merchantName = item.merchantName;//商户名称
                String merchantNo = item.merchantNo;//商户号
                String bpName = item.bpName;//机具类型
                String agentNameParent = item.agentNameParent;//所属代理商名称
                String agentNameOne = item.agentNameOne;//一级代理商
                String mobilephone = item.mobilephone;//收货人手机号
                String address = item.address;//收货人地址
                String status = item.status;//状态 0:待直属处理  1:已处理  2:待一级处理
                String updateTime = item.updateTime;//处理时间
                String remark = item.remark;//处理备注
                String agentNoParent = item.agentNoParent;//所属代理商编号
                String agentNoOne = item.agentNoOne;//一级代理商编号
                String sn = item.sn;//机具sn号
                String isForce = "否";
                if (!TextUtils.isEmpty(sn))
                 isForce = "是";//是否强制下发机具号--- (只要有SN， 就是强制下发)
                if (status.equals("0"))
                    status = "待直属处理";
                else if (status.equals("1"))
                    status = "已处理";
                else if (status.equals("2"))
                    status = "待一级处理";

                Bundle bundle = new Bundle();
                bundle.putString("record_id",record_id);//记录id 需要传给后台处理的
                bundle.putString("needOperation",needOperation);//是否显示处理按钮
                bundle.putString("createTime",createTime);
                bundle.putString("merchantName",merchantName);
                bundle.putString("merchantNo",merchantNo);
                bundle.putString("bpName",bpName);
                bundle.putString("agentNameParent",agentNameParent);
                bundle.putString("agentNameOne",agentNameOne);
                bundle.putString("mobilephone",mobilephone);
                bundle.putString("address",address);
                bundle.putString("status",status);
                bundle.putString("updateTime",updateTime);
                bundle.putString("remark",remark);
                bundle.putString("agentNoParent",agentNoParent);
                bundle.putString("agentNoOne",agentNoOne);
                bundle.putString("sn",sn);
                bundle.putString("isForce",isForce);//是否强制下发机具号
                intent.putExtras(bundle);
                startActivity(intent);
            }

            @Override
            public void onItemLongClick(View view, ManagementCenterProto.TerminalApplyRecord item, int position) {
                //not donothing
            }
        });
        lv_record = (CommonLinerRecyclerView) view.findViewById(R.id.lv_deviceRecord);
        lv_record.setAdapter(adapter);
        refreshLayout = (RefreshLayout)view.findViewById(R.id.refreshLayout);
        //设置 Header 和 Footer
        refreshLayout.setRefreshHeader(new ClassicsHeader(mContext).setSpinnerStyle(SpinnerStyle.Translate));
        refreshLayout.setRefreshFooter(new ClassicsFooter(mContext).setSpinnerStyle(SpinnerStyle.Translate));
        refreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                if (mTotal > currPage * GetTerminalApplyRecordAPI.PAGESIZE) {//如果还小于总的大小；接着请求下10条记录
                    currPage++;
                    Log.d("ApplyforDevicesFragment", "onLoadmore: "+mRecordStatus);
                    sendHttpReq(mRecordStatus);
                    refreshlayout.finishLoadmore(1000);
                } else {
                    showToast("已经是最后一页了");
                    refreshlayout.finishLoadmore(1000);
                }
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {//刷新
                currPage = 1;
                sendHttpReq(mRecordStatus);
                Log.d("ApplyforDevicesFragment", "onRefresh: "+mRecordStatus);
                refreshlayout.finishRefresh(1000);
            }
        });
//        refreshLayout.autoRefresh();
    }

    @Override
    public void onResume() {
        super.onResume();
        refreshLayout.autoRefresh();
    }

    /**
     * 开始网络请求
     *
     * @param recordStatus
     */
    private void sendHttpReq(@TerminalNoTypeDef.TerminalNoType final String recordStatus)
    {
        mTerminalApplyRecordAPI.getTerminalApplyRecordInfo(currPage,recordStatus);
    }
}
